<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary">增加停车计费规则</el-button>

      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ scope.row.chargeType }}
          </template>
        </el-table-column>

        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>

            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
/*
 开发列表数据
 1. ✔️在api/新建一个car.js  -> 封装一个请求接口的方法
  // 接口说明地址:https://apifox.com/apidoc/shared-04b75e91-2e76-4a88-8774-6e5d65d14b61/api-128582323
 2. 在页面加载后的生命周期方法中调用接口
 1. created  2. mounted  都可以调用请求获取数据,只不过created执行先于 mounted而已，能尽快拿到数据
  - methods中定义一个请求方法getRuleList
  - mounted中调用这个getRuleList方法
*/
import { getRuleListAPI } from "@/api/car";
import { utils, writeFileXLSX } from "xlsx";
export default {
  name: "Building",
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
    };
  },
  methods: {
    // 定义导出数据的方法
    async exportToExcel() {
      // 1. 请求服务器获取数据
      const res = await getRuleListAPI({ page: 1, pageSize: 10 });
      // console.log("dddd", res);

      const rows = res.data.rows;
      // 2. 准备表头数组
      // 表头英文字段key
      const tableHeaderKeys = [
        "ruleNumber",
        "ruleName",
        "freeDuration",
        "chargeCeiling",
        "chargeType",
        "ruleNameView",
      ];
      // 表头中文字段value
      const tableHeaderValues = [
        "计费规则编号",
        "计费规则名称",
        "免费时长(分钟)",
        "收费上线(元)",
        "计费方式",
        "计费规则",
      ];
      // 以excel表格的顺序调整后端数据
      // 如何将rows对象数组中的数据过滤成tableHeaderKeys中的属性
      let newRows = [];
      rows.forEach((row) => {
        let newRow = {};
        tableHeaderKeys.forEach((key) => {
          // 中括号赋值法
          newRow[key] = row[key];
        });
        newRows.push(newRow);
      });

      // 创建一个新的工作簿
      const workbook = utils.book_new();
      // 创建一个工作表 要求一个对象数组格式
      const worksheet = utils.json_to_sheet(newRows);

      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, "Data");
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: "A1" })
      // 导出方法进行导出
      writeFileXLSX(workbook, "data.xlsx");
    },
    async getRuleList() {
      // 1. 调用
      const res = await getRuleListAPI({ page: 1, pageSize: 10 });
      // 2. 获取数据赋值给ruleList
      this.ruleList = res.data.rows;
      console.log(res);
    },
  },
  mounted() {
    this.getRuleList();
  },
};
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
